/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.beans.java;

import ca.trader.beans.enterprise.SaleBeanLocal;
import ca.trader.except.SaleDoesNotExistException;
import ca.trader.logger.TraderLogger;
import ca.trader.regions.Location;
import ca.trader.sale.Sale;
import ca.trader.sale.SaleCategoryEnum;
import java.math.BigDecimal;
import java.util.Date;

/**
 *
 * @author Ahsan
 */
public class SaleLookupByIDBean
{
	 private static final String _className = SaleLookupByIDBean.class.getName();
	 
	 private Sale sale;
	 private boolean foundResults;
	 
	 public void setSaleID (String saleID)
	 {
		  
		  final String _methodName = "setSaleID";

		  TraderLogger.logMethodEntry(_className, _methodName);

		  TraderLogger.logInfo(_className, _methodName, "Finding sale with ID: " + saleID);

		  // if the ID is null
		  if (saleID == null)
		  {
			   TraderLogger.logInfo(_className, _methodName, "ID is null. Doing nothing");
		  }
		  else
		  {
			   try
			   {

				    sale = null;

				    Integer id = Integer.parseInt(saleID);

				    SaleBeanLocal saleBean = BeanUtils.getSaleBean();
				    
				    // Get the user with the ID
				    sale = saleBean.getSaleByID(id);

				    TraderLogger.logInfo(_className, _methodName, "Found the sale");
				    
				    foundResults = true;

			   }
			   // If the id is not a number
			   catch (NumberFormatException nfe)
			   {
				    TraderLogger.logWarning(_className, _methodName, "ID is not an integer.  Doing nothing.");
			   }
			   // If no sale found with the ID
			   catch (SaleDoesNotExistException nfe)
			   {
				    TraderLogger.logWarning(_className, _methodName, "No sale found with the matching ID.  Doing nothing.");
			   }
			   // Other exception
			   catch (Exception e)
			   {
				    TraderLogger.logSevere(_className, _methodName, "Unknown exception ", e);
			   }
		  }
		  
		  TraderLogger.logMethodExit(_className, _methodName);

	 }
	 
	 public boolean isResultsFound()
	 {
		  return foundResults;
	 }
	 
	 public Sale getSale()
	 {
		  if (!foundResults)
		  {
			   throw new IllegalStateException("A sale has not been found.");
		  }
		  
		  return sale;
		  
	 }
	 
	 public String getDescription()
	 {
		  if (!foundResults)
		  {
			   throw new IllegalStateException("A sale has not been found.");
		  }
		  
		  return sale.getDescription();
		  
	 }
	 
	 public Location getLocation()
	 {
		  if (!foundResults)
		  {
			   throw new IllegalStateException("A sale has not been found.");
		  }
		  
		  return sale.getLocation();
	 }
	 
	 public String getTitle ()
	 {
		  if (!foundResults)
		  {
			   throw new IllegalStateException("A sale has not been found.");
		  }
		  
		  return sale.getTitle();
	 }
	 
	 public BigDecimal getPrice ()
	 {		  
		  if (!foundResults)
		  {
			   throw new IllegalStateException("A sale has not been found.");
		  }
		  
		  return sale.getPrice();
	 }
	 
	 public Date getCreatedDate ()
	 {
		  if (!foundResults)
		  {
			   throw new IllegalStateException("A sale has not been found.");
		  }
		  
		  return sale.getCreationDate();
	 }
	 
	 public Date getModifiedDate ()
	 {
		  if (!foundResults)
		  {
			   throw new IllegalStateException("A sale has not been found.");
		  }
		  
		  return sale.getModifiedDate();
	 }
	 
	 public Integer getSellerID()
	 {
		  if (!foundResults)
		  {
			   throw new IllegalStateException("A sale has not been found.");
		  }
		  
		  return sale.getSellerID();
	 }
	 
	 public String getSellerFullName ()
	 {
		  if (!foundResults)
		  {
			   throw new IllegalStateException("A sale has not been found.");
		  }
		  
		  return sale.getSeller().toString();
		  
	 }
	 
	 public SaleCategoryEnum getSaleCategory ()
	 {
		  if (!foundResults)
		  {
			   throw new IllegalStateException("A sale has not been found.");
		  }
		  
		  return sale.getCategory();
	 }
}
